﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using G15_Agent.Helpers.ServiceHelpers;
using G15_Agent.Helpers.ServiceHelpers.HotelServiceHelpers;
using G15_Agent.Models.ViewModels.HotelViewModels;
using G15_Agent.Models.Hotel;
using G15_Agent.Models;
using G15_Agent.Helpers;

namespace G15_Agent.Controllers
{
    public class HotelController : Controller
    {
        private AgentContext DatabaseSQL = new AgentContext();

        public ActionResult Index()
        {
            List<string> listService = new List<string>();
            foreach (var service in ServiceFactory.Hotel)
            {
                listService.Add(service.ServiceID);
            }

            return View(listService);
        }

        public ActionResult HotelSearch(string serviceID)
        {
            bool isError = false;
            var service = (HotelService)ServiceFactory.GetService(serviceID);
            SearchHotelViewModel viewModel = new SearchHotelViewModel();

            try
            {
                viewModel.dsHangPhong = service.GetClassOfRoomList();
                viewModel.dsLoaiPhong = service.GetTypeOfRoomList();
                viewModel.serviceID = serviceID;
                //viewModel.tgDen = System.DateTime.Today;
                //viewModel.tgDi = System.DateTime.Today;
                viewModel.tgDen = new DateTime(2012, 11, 1);
                viewModel.tgDi = new DateTime(2012, 11, 2);
            }
            catch (Exception ex)
            {
                isError = true;
            }            

            //Kiem tra loi khi ket noi voi ws
            if (isError || viewModel.dsHangPhong == null || viewModel.dsLoaiPhong == null || serviceID == null)
            {
                ViewBag.Errors = @"Có lỗi xãy ra khi kết nối với bên dịch vụ cung cấp";
                return View();
            }

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult HotelSearch(SearchHotelViewModel data)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("HotelList", new {
                    serviceID = data.serviceID,
                    maHangPhong = data.maHangPhong,
                    maLoaiPhong = data.maLoaiPhong,
                    tgDen = data.tgDen.ToShortDateString(),
                    tgDi = data.tgDi.ToShortDateString()
                });
            }

            return View();
        }

        public ActionResult HotelList(string serviceID, string maHangPhong, string maLoaiPhong, string tgDen, string tgDi)
        {
            var service = (HotelService)ServiceFactory.GetService(serviceID);
            ListHotelViewModel viewModel = new ListHotelViewModel();

            try
            {
                viewModel.dsPhong = service.RetrieveRoomList(maHangPhong, maLoaiPhong, DateTime.Parse(tgDen), DateTime.Parse(tgDi));
                viewModel.serviceID = serviceID;
                viewModel.tgDen = DateTime.Parse(tgDen);
                viewModel.tgDi = DateTime.Parse(tgDi);
            }
            catch (Exception ex)
            {
                ViewBag.Errors = @"Không tìm thấy phòng theo yêu cầu";
                return View();
            }

            return View(viewModel);
        }

        [Authorize]
        public ActionResult BookRoom(string roomClass, string roomType, string serviceID, string tgDen, string tgDi, string gia)
        {
            ListHotelViewModel data = new ListHotelViewModel();
            data.tgDen = DateTime.Parse(tgDen);
            data.tgDi = DateTime.Parse(tgDi);
            data.serviceID = serviceID;
            data.dsPhong = new List<Phong>();
            data.dsPhong.Add(new Phong{
                maHangPhong = roomClass,
                maLoaiPhong = roomType,
                soLuong = 1,
                giaPhong = double.Parse(gia)
            });


            var service = (HotelService)ServiceFactory.GetService(data.serviceID);
           
            string maHoaDon = "pending";
            try
            {
                maHoaDon = service.BookingForRoom(data.dsPhong, data.tgDen, data.tgDi);
            }
            catch (Exception ex)
            {
                ViewBag.Error("Xảy ra lỗi khi tiến hành đặt phòng");
            }

            BookingHotelViewModel viewModel = new BookingHotelViewModel();
            viewModel.dsPhong = data.dsPhong;
            viewModel.serviceID = service.ServiceID;
            viewModel.maHoaDon = maHoaDon;
            viewModel.hangPhong = roomClass;
            viewModel.loaiPhong = roomType;
            viewModel.tgDen = data.tgDen.ToShortDateString();
            viewModel.tgDi = data.tgDi.ToShortDateString();

            string orderDetail = 
                "Khách sạn: " + DatabaseHelper.GetServiceName(serviceID) +
                "Hạng phòng: " + viewModel.hangPhong +
                "Loại phòng: " + viewModel.loaiPhong +
                "Ngày nhận: " + viewModel.tgDen + 
                "Ngày trả: " + viewModel.tgDi;

            DatabaseHelper.AddOrderDetail(
                viewModel.serviceID, 
                DatabaseHelper.GetUserID(User.Identity.Name), 
                orderDetail, 
                (int)viewModel.dsPhong.First().giaPhong);

            return View(viewModel);
        }

    }
}
